// pages/register/index.js

var app = getApp();
var CommonUtils = app.CommonUtils;
var Keys = require('../../assets/data/keys')
const Toptips = require('../../dist/toptips/index');

Page(Object.assign({}, Toptips, {

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    codeText: '',
    userPassword: '',
    codeDis: false,
    btnCodeText: "发送验证码",
    openid: '',
    showSecond: false,   //是否显示倒计时单位  0：不现实  1：显示
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },



  /**
   * 用户名输入
   */
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  /**
   * 验证码输入
   */
  userCodeInput: function (e) {
    this.setData({
      codeText: e.detail.value
    })
  },


  /**
   * 用户密码输入
   */
  userPasswordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    })
  },

  /**
   * 发送验证码
   */
  btnCode: function (e) {
    var that = this
    if (!CommonUtils.isPoneAvailable(that.data.userName)) {
      that.showZanTopTips("请输入有效的手机号");
      return;
    }
    that.setData({
      codeDis: true
    })

    wx.request({
      url: Keys.data.url_login,
      data: {
        DataType: "Register_Password",
        tel: that.data.userName,
      },
      success: function (res) {
        console.log(res)
        if (res.data.isok == "true") {
          that.setData({
            btnCodeText: 10,
            showSecond: true
          })
          let time = setInterval(() => {
            let btnCodeText = that.data.btnCodeText
            btnCodeText--
            that.setData({
              btnCodeText: btnCodeText
            })
            if (btnCodeText == 0) {
              clearInterval(time)
              that.setData({
                btnCodeText: "获取验证码",
                codeDis: false,
                showSecond: false
              })
            }
          }, 1000)
        }
        else {
          that.showZanTopTips(res.data.info);
          that.setData({
            codeDis: false
          })
        }
      }
    })
  },


  /**
   * 确认按钮
   */
  btnConfrim: function (e) {
    if (this.data.userName.length == 0 || this.data.codeText.length == 0 || this.data.userPassword.length == 0) {
      this.showZanTopTips("请完善注册信息");
      return
    }
    var _this = this;
    wx.request({
      url: Keys.data.url_login,
      data: {
        DataType: "Register_RecoveredPassword",
        tel: _this.data.userName,
        number: _this.data.codeText,
        password: _this.data.userPassword,
      },
      success: function (res) {
        console.log(res)
        if (res.data.isok == "true") {
          CommonUtils.showToastWithImg("修改成功", "1")
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          _this.showZanTopTips(res.data.info);
        }
      }
    })
  },
}))